<?php

namespace app\admin\controller\yxerp;

use app\common\controller\Backend;
use think\db\exception\PDOException;
use think\facade\Db;

/**
 * 岗位管理
 */
class Position extends Backend
{
    /**
     * Position模型对象
     * @var object
     * @phpstan-var \app\admin\model\yxerp\Position
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['id', 'name', 'code'];

    protected string|array $withJoin = [
        'department' => ['name'],
    ];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\yxerp\Position();
    }

    /**
     * 查看
     */
    public function index(): void
    {

    $this->request->filter(['strip_tags', 'trim']);


        if ($this->request->param('select')) {
            $this->model->removeOption('page');
        }

    list($where, $alias, $limit, $order) = $this->queryBuilder();
    $departmentId = $this->request->param('department_id');
    if ($departmentId) {
        $where[] = ['department_id', '=', $departmentId];
    }

    $res = $this->model
        ->alias($alias)
        ->where($where)
        ->order($order)
        ->paginate($limit);

    $this->success('', [
        'list'   => $res->items(),
        'total'  => $res->total(),
        'remark' => '',
    ]);
    }
    /**
     * 添加
     */
    public function add(): void
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data = $this->excludeFields($data);
            $result = false;
            Db::startTrans();
            try {
                // 是否采用模型验证
                if ($this->modelValidate) {
                    $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                    $this->model->validate($validate);
                }
                $result = $this->model->save($data);
                Db::commit();
            } catch (PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Added successfully'));
            } else {
                $this->error(__('No rows were added'));
            }
        }

        $this->error(__('Parameter error'));
    }

    /**
     * 编辑
     */
    public function edit(): void
    {
        $id = $this->request->param($this->model->getPk());
        $row = $this->model->find($id);
        if (!$row) {
            $this->error(__('Record not found'));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data = $this->excludeFields($data);
            $result = false;
            Db::startTrans();
            try {
                // 是否采用模型验证
                if ($this->modelValidate) {
                    $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                    $this->model->validate($validate);
                }
                $result = $row->save($data);
                Db::commit();
            } catch (PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Update successful'));
            } else {
                $this->error(__('No rows updated'));
            }
        }

        $this->success('', [
            'row' => $row
        ]);
    }

    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
}